﻿/*
   Copyright 2012 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using Flower.Services.Data;
using NUnit.Framework;

namespace Flower.Directory.Util.Tests
{
    [TestFixture]
    public class BlobUtilTests
    {
        [DataContract]
        private class Data
        {
            [DataMember]
            public int Prop1 { get; set; }

            [DataMember]
            public string Prop2 { get; set; }

            [DataMember]
            public DateTime Prop3 { get; set; }

            [DataMember]
            public byte[] Bytes { get; set; }
        }

        [Test]
        public void SerializeDeserialize()
        {
            var data = new Data
            {
                Prop1 = 1,
                Prop2 = "abc",
                Prop3 = DateTime.Now,
                Bytes = new byte[1024]
            };

            var blob = data.ToBlob(BlobFormat.TextXml);
            var deserData = blob.To<Data>();

            Assert.IsTrue(deserData.Prop1 == data.Prop1);
            Assert.IsTrue(deserData.Prop2 == data.Prop2);
            Assert.IsTrue(deserData.Prop3 == data.Prop3);

            Assert.IsTrue(blob.Format == BlobFormat.TextXml);
            Assert.IsTrue(blob.Data.Length > 0);
            //Console.WriteLine("BlobWrapperTests: Plain XML size: " + blob.Data.Length);

            blob = data.ToBlob(BlobFormat.BinXml);
            deserData = blob.To<Data>();

            Assert.IsTrue(deserData.Prop1 == data.Prop1);
            Assert.IsTrue(deserData.Prop2 == data.Prop2);
            Assert.IsTrue(deserData.Prop3 == data.Prop3);

            Assert.IsTrue(blob.Format == BlobFormat.BinXml);
            Assert.IsTrue(blob.Data.Length > 0);
            //Console.WriteLine("BlobWrapperTests: Binary XML size: " + blob.Data.Length);

            blob = data.ToBlob(BlobFormat.GZipXml);
            deserData = blob.To<Data>();

            Assert.IsTrue(deserData.Prop1 == data.Prop1);
            Assert.IsTrue(deserData.Prop2 == data.Prop2);
            Assert.IsTrue(deserData.Prop3 == data.Prop3);

            Assert.IsTrue(blob.Format == BlobFormat.GZipXml);
            Assert.IsTrue(blob.Data.Length > 0);
            //Console.WriteLine("BlobWrapperTests: GZiped XML size: " + blob.Data.Length);
        }
    }
}
